//
//  ZFLayerModal.swift
//  map
//
//  Created by fyzq on 2024/1/30.
//

import UIKit
import RealmSwift
import HandyJSON
class ZFLayerModal: Object {

    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var uuid:String = ""
    @Persisted var mapName:String = ""
    @Persisted var layerUrl:String?
    @Persisted var picture:String?
    @Persisted var type:Int = 1
    @Persisted var remark:String = ""

}


extension ZFLayerModal {
    
    ///创建默认图源
   static func setupDefault(){
       let predicate = NSPredicate(format: "uuid == ''")
       guard let layers = ZFRealmManager.objectsWithPredicate(object: ZFLayerModal.self, predicate: predicate),layers.count > 0 else {
           ///创建默认源
           return onCreateDefault()
       }
    }
    fileprivate static func onCreateDefault(){
        let modal = ZFLayerModal()
        modal.mapName = "街景图"
        modal.layerUrl = "mapbox://styles/su1qun/clrzyv6zg00ax01pqf5yodwhh"
        
        modal.picture = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v12/static/[-77.043686,38.892035,-77.028923,38.904192]/400x200?access_token=pk.eyJ1Ijoic3UxcXVuIiwiYSI6ImNsYmJuajMyMjFnczAzb3FwczYxa3p5aGUifQ.7YT7YKbFF0eHxEfTQfpu_A"
        modal.remark = "这是街景"
        
        let modal1 = ZFLayerModal()
        modal1.mapName = "卫星图"
        modal1.layerUrl = "mapbox://styles/su1qun/clrzz6swy00a601r90n2aasho"
        
        modal1.picture = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v12/static/[-77.043686,38.892035,-77.028923,38.904192]/400x200?access_token=pk.eyJ1Ijoic3UxcXVuIiwiYSI6ImNsYmJuajMyMjFnczAzb3FwczYxa3p5aGUifQ.7YT7YKbFF0eHxEfTQfpu_A"
        modal1.remark = "这是卫星图"
        
        let modal2 = ZFLayerModal()
        modal2.mapName = "户外图"
        modal2.layerUrl = "mapbox://styles/su1qun/clrzslny1006y01paeq4rcr77"
        
        modal2.picture = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v12/static/[-77.043686,38.892035,-77.028923,38.904192]/400x200?access_token=pk.eyJ1Ijoic3UxcXVuIiwiYSI6ImNsYmJuajMyMjFnczAzb3FwczYxa3p5aGUifQ.7YT7YKbFF0eHxEfTQfpu_A"
        modal2.remark = "这是户外"
        
        let layers = [modal,modal1,modal2]
        ZFRealmManager.addList(layers, task: {
           print("创建默认图层成功")
        })
    }
    
    
    

    
    ///获取一个用户的layers
    static func getLayers()->[ZFLayerModal]{
        ///现获取连接表
        let uuid = String.UUID()
//        let links = ZFAccountToLayerModal.onGetAccountToLayerTabel(uuid: uuid)!;
        
//        let layerIds =  links.map { link in
//            return  link.layerId
//        } as! [ObjectId]
//        let predicate = NSPredicate(format: "_id IN %@", layerIds)
        let predicate = NSPredicate(format: "uuid == %@ OR uuid == ''", uuid)
        guard let layers = ZFRealmManager.objectsWithPredicate(object: ZFLayerModal.self, predicate: predicate),layers.count > 0 else {
            ///查询不到用户 创建用户
            return []
        }
        return layers as! [ZFLayerModal]
    }

}
